import shapefile


r = shapefile.Reader("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_UTM")
w = shapefile.Writer("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_02")
geo = shapefile.Reader("shapefiles/NYC_MUSEUMS_GEO")
# 1.添加字段
w.fields = list(r.fields[1:])
w.field("GEO_LAT", "F", 5, 8)
w.field("GEO_LON", "F", 5, 8)
# [('DeletionFlag', 'C', 1, 0),
# ['NAME', 'C', 254, 0],
# ['TEL', 'C', 254, 0],
# ['URL', 'C', 254, 0],
# ['ADRESS1', 'C', 254, 0],
# ['ADDRESS2', 'C', 254, 0],
# ['CITY', 'C', 254, 0],
# ['ZIP', 'N', 19, 8]]

# 2.复制记录
for i in range(geo.numRecords):
    lon, lat = geo.shape(i).points[0]
    shapeRecord = r.record(i)
    # 方法 1:
    shapeRecord.extend([lat, lon])
    w.record(*shapeRecord)
    # 方法 2:
    # w.record(NAME=shapeRecord[0], TEL=shapeRecord[1], URL=shapeRecord[2],
    #          ADRESS1=shapeRecord[3], ADDRESS2=shapeRecord[4], CITY=shapeRecord[5],
    #          ZIP=shapeRecord[6], GEO_LAT=lat, GEO_LON=lon)
# 3.复制图形
for item in r.iterShapes():
    x, y = item.points[0]
    w.point(x, y)

print(r.record(0))
w.close()
r.close()

# 验证添加的字段
r = shapefile.Reader("shapefiles/NYC_MUSEUMS_02")

print(r)
print(r.record(0))
r.close()